package com.example.wilson.lifediary;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class MainActivity extends ListActivity {

    private SharedPreferences textpassword = null;
    private String password = null;
    private boolean isSet = false;
    private EditText checkpass = null;

    //检查是否第一次进入应用
    boolean isFirstIn = false;

    //插入一条新记录
    public static final int MENU_ITEM_INSERT = Menu.FIRST;

    //编辑内容
    public static final int MENU_ITEM_EDIT = Menu.FIRST + 1;


    //GreenDAO使用的变量
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;
    private ListView item;
    private Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView item = getListView();

        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent0 = new Intent(MainActivity.this, DiaryEditor.class);
                intent0.setAction(DiaryEditor.INSERT_DIARY_ACTION);
                intent0.setData(getIntent().getData());
                startActivity(intent0);
                MainActivity.this.finish();
                cursor.requery();
            }
        });

        item.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long id) {
                //Toast.makeText(MainActivity.this, "跳转成功", Toast.LENGTH_LONG).show();
                noteDao.deleteByKey(id);
                Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();
                InitList();
                return true;
            }
        });
        InitDAO();
        InitList();
    }

    /**
     * 初始化DAO
     */
    private void InitDAO(){
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        noteDao = daoSession.getNoteDao();
    }

    private void InitList(){
        String textColumn = NoteDao.Properties.Titile.columnName;
        String dateColumn = NoteDao.Properties.Date.columnName;
        String orderBy = dateColumn + " COLLATE LOCALIZED DESC";
        cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, orderBy);
        String[] from = {
                textColumn, dateColumn
        };
        int[] to = {R.id.text1, R.id.created};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.diary_row, cursor, from, to);
        setListAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, MENU_ITEM_INSERT, 0, R.string.menu_insert);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case MENU_ITEM_INSERT :
                Intent intent0 = new Intent(this, DiaryEditor.class);
                intent0.setAction(DiaryEditor.INSERT_DIARY_ACTION);
                intent0.setData(getIntent().getData());
                startActivity(intent0);
                MainActivity.this.finish();
                cursor.requery();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String mid = Long.toString(id);
        Intent intent = new Intent(this, DiaryEditor.class);
        intent.setAction(DiaryEditor.EDIT_DIARY_ACTION);
        Bundle bundle = new Bundle();
        bundle.putLong("id", id);
        intent.putExtras(bundle);
        Log.d("id", mid);
        startActivity(intent);
        MainActivity.this.finish();
        cursor.requery();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        cursor.requery();
        super.onActivityResult(requestCode, resultCode, data);
    }
}
